package com.yuanlrc.hotel.restaurantPicture.controller;


import java.util.List;


import com.yuanlrc.hotel.restaurant.domain.Restaurant;
import com.yuanlrc.hotel.restaurant.service.IRestaurantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.yuanlrc.common.annotation.Log;
import com.yuanlrc.common.enums.BusinessType;
import com.yuanlrc.hotel.restaurantPicture.domain.RestaurantPicture;
import com.yuanlrc.hotel.restaurantPicture.service.IRestaurantPictureService;
import com.yuanlrc.common.core.controller.BaseController;
import com.yuanlrc.common.core.domain.AjaxResult;
import com.yuanlrc.common.utils.poi.ExcelUtil;
import com.yuanlrc.common.core.page.TableDataInfo;

import javax.servlet.http.HttpSession;

/**
 * 餐厅图库Controller
 * 
 * @author yuanlrc
 * @date 2020-11-18
 */
@Controller
@RequestMapping("/hotel/restaurantPicture")
public class RestaurantPictureController extends BaseController {
    private String prefix = "hotel/restaurantPicture";

    @Autowired
    private IRestaurantPictureService restaurantPictureService;
    @Autowired
    private IRestaurantService restaurantService;

    @RequiresPermissions("hotel:restaurantPicture:view")
    @GetMapping()
    public String restaurantPicture()
    {
        return prefix + "/restaurantPicture";
    }

    /**
     * 查询餐厅图库列表
     */
    @RequiresPermissions("hotel:restaurantPicture:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RestaurantPicture restaurantPicture) {
        startPage();
        List<RestaurantPicture> list = restaurantPictureService.selectRestaurantPictureList(restaurantPicture);
        for (RestaurantPicture picture : list) {
            Restaurant restaurant = restaurantService.selectRestaurantById(picture.getFid());
            picture.setRestaurant(restaurant);
        }
        return getDataTable(list);
    }

    /**
     * 导出餐厅图库列表
     */
    @RequiresPermissions("hotel:restaurantPicture:export")
    @Log(title = "餐厅图库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RestaurantPicture restaurantPicture) {
        List<RestaurantPicture> list = restaurantPictureService.selectRestaurantPictureList(restaurantPicture);
        ExcelUtil<RestaurantPicture> util = new ExcelUtil<RestaurantPicture>(RestaurantPicture.class);
        return util.exportExcel(list, "restaurantPicture");
    }

    /**
     * 新增餐厅图库
     */
    @GetMapping("/add")
    public String add() {
        HttpSession session = getSession();
        List<Restaurant> restaurants = restaurantService.selectRestaurantList(new Restaurant());
        session.setAttribute("restaurants",restaurants);
        return prefix + "/add";
    }

    /**
     * 新增保存餐厅图库
     */
    @RequiresPermissions("hotel:restaurantPicture:add")
    @Log(title = "餐厅图库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave( RestaurantPicture restaurantPicture) {
        //判断图片是否有所属的餐厅
        if (restaurantPicture.getFid()==null||restaurantPicture.getFid()==0){
            return toAjax(false);
        }
        int count = 0;
        try{
            count = restaurantPictureService.insertRestaurantPicture(restaurantPicture);
        }catch (Exception e){
           return toAjax(false);
        }
        return toAjax(count);
    }

    /**
     * 修改餐厅图库
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        List<Restaurant> restaurants = restaurantService.selectRestaurantList(new Restaurant());
        RestaurantPicture restaurantPicture = restaurantPictureService.selectRestaurantPictureById(id);
        Restaurant restaurant = restaurantService.selectRestaurantById(restaurantPicture.getFid());
        mmap.put("restaurantPicture", restaurantPicture);
        mmap.put("restaurants",restaurants);//所有餐厅列表
        mmap.put("restaurant",restaurant);//此图片所属餐厅
        return prefix + "/edit";
    }

    /**
     * 修改保存餐厅图库
     */
    @RequiresPermissions("hotel:restaurantPicture:edit")
    @Log(title = "餐厅图库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RestaurantPicture restaurantPicture){
        if(restaurantPicture.getId()==null||restaurantPicture.getId()==0){
            return error("编辑失败");
        }
        int count = 0;
        try{
            if (restaurantPicture.getFid()==null||restaurantPicture.getFid()==0){
                return toAjax(false);
            }
            count = restaurantPictureService.updateRestaurantPicture(restaurantPicture);
        }catch (Exception e){
            return toAjax(false);
        }
        return toAjax(count);
    }

    /**
     * 删除餐厅图库
     */
    @RequiresPermissions("hotel:restaurantPicture:remove")
    @Log(title = "餐厅图库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int count = 0;
        try{
            count = restaurantPictureService.deleteRestaurantPictureByIds(ids);
        }catch (Exception e){
            return toAjax(false);
        }
        return toAjax(count);
    }
}
